package com.gitee.wsl.time

import com.gitee.wsl.ext.base.TimestampClock
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.TestCoroutineScheduler
import kotlinx.coroutines.test.TestScope
import kotlin.time.Instant

import kotlin.time.ExperimentalTime

class TestTimestampClock(private val scheduler: TestCoroutineScheduler) : TimestampClock {

    @OptIn(ExperimentalTime::class, ExperimentalCoroutinesApi::class)
    override val currentTime: Instant
        get() = scheduler.currentTime.toInstantFromMillisecondsSinceEpoch()

    @OptIn(ExperimentalCoroutinesApi::class)
    override val currentTimeMilliseconds: Long
        get() = scheduler.currentTime

    @OptIn(ExperimentalCoroutinesApi::class)
    override val currentTimeSecond: Long
        get() = scheduler.currentTime / T.MILLISECONDS_PER_SECOND
}

fun TestScope.useTestClock() {
    com.gitee.wsl.ext.base.timestampClock = TestTimestampClock(testScheduler)
}